import * as CONSTANT from '../constant'
import axios from 'axios';

export const  syncAction = (inputTask)=>{
    return {type:CONSTANT.TASK_SYNC,payload:inputTask} 
}

export const addAction = ()=>{
    return {type:CONSTANT.TASK_ADD} 
}

export const deleteAction = (index)=>{
    return {type:CONSTANT.TASK_DELETE,payload:index} 
}

//异步操作：网络请求
export const actionInitTasksAsync = ()=>{
    //返回是一个函数，函数体内做网络请求；
    //dispatch 派发action，getState 获取store的state
    return (dispatch,getState)=>{

        // console.log('state',getState());
       
        axios.get('http://localhost:3001/tasks')
        .then(res=>{
   
            const tasks = res.data
            console.log(tasks);

            dispatch({type:CONSTANT.TASK_INIT,payload:tasks})
        })
    }

   
}


